package com.kof.userregister.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.kof.userregister.entity.Address;
import com.kof.userregister.entity.Employee;

public class OgnlAction extends BaseAction implements ParameterAware,RequestAware,SessionAware,ApplicationAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7893366367952289857L;

	private Map parameters;
	private Map request;
	private Map session;
	private Map application;
	
	private List<Employee> emps = new ArrayList<Employee>();
	
	/**
	 * 初始化雇员信息，将Employee对象保存到List对象emps中
	 */
	public void initEmps()
	{
		Address address1 = new Address("中国","北京","海淀南路18号");
		Address address2 = new Address("中国","上海","南京西路20号");
		Address address3 = new Address("中国","成都","春熙路8号");
		
		String[] emails1 = new String[]{"zhangsan@sina.com","zhangsan@163.com"};
		String[] emails2 = new String[]{"lisi@sina.com","lisi@163.com"};
		String[] emails3 = new String[]{"wangwu@sina.com","wangwu@163.com"};
		
		Map<String,String> accounts1 = new HashMap<String,String>();
		accounts1.put("建行", "12345678");
		accounts1.put("工行", "987654");
		
		Map<String,String> accounts2 = new HashMap<String,String>();
		accounts2.put("中行", "234567");
		accounts2.put("农行", "597432");
		
		Map<String,String> accounts3 = new HashMap<String,String>();
		accounts3.put("招行", "343217");
		accounts3.put("中信银行", "213419");
		
		Employee emp1 = new Employee("张三",30,1900f,address1,emails1,accounts1);
		Employee emp2 = new Employee("李四",26,2200f,address2,emails2,accounts2);
		Employee emp3 = new Employee("王五",28,2600f,address3,emails3,accounts3);
		
		emps.add(emp1);
		emps.add(emp2);
		emps.add(emp3);
	}

	@Override
	public void setApplication(Map application) {
		// TODO Auto-generated method stub
		this.application=application;
		
	}

	@Override
	public void setSession(Map session) {
		// TODO Auto-generated method stub
		this.session=session;
	}

	@Override
	public void setRequest(Map request) {
		// TODO Auto-generated method stub
		this.request=request;
	}

	@Override
	public void setParameters(Map parameters) {
		// TODO Auto-generated method stub
		this.parameters=parameters;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		//调用initEmps()方法，初始化雇员信息
		initEmps();
		
		//在parameters对象中添加欢迎信息
		parameters.put("greeting", "欢迎访问程序员之家");
		
		//在request对象中添加雇员1的姓名（张三）
		request.put("empName", emps.get(0).getName());
		
		//在session对象中添加雇员2的姓名（李四）
		session.put("empName", emps.get(1).getName());
		
		//在application对象中添加雇员3的姓名（王五）
		application.put("empName", emps.get(2).getName());
		
		return SUCCESS;
	}
	
	public List<Employee> getEmps()
	{
		return emps;
	}
}
